/***************************************************************************************************
*Copyright(C),2017-2020,Sumscope
*FileName	:  FixedNatFutureData
*Author	:  Givins.zhu
*Version	:  2.0
*Date		:  2019/10/11
*Desc		:
*History	:
*Email    :  Givins.zhu@sumscope.com
***************************************************************************************************/
#pragma  once
#include <QString>
#include <map>
#include <set>
#include <qbprotocol/include/SSQBModel.h>
#include <qbprotocol/include/SSMsgStruct.h>
#include <qbmessage/include/message_bond.h>
#include <qbtools/message/MessagePoster.h>
#include <core/Events.h>

typedef struct _stFuturePriceVolumn{
	_stFuturePriceVolumn(){
		m_fPrice = 0.0;
		m_dbOpenInte = 0.0f;
		m_nSec = m_nVolumn = 0;
	}
	_stFuturePriceVolumn(float prc, int vol, double dbOpenInte, int sec){
		m_fPrice = prc;
		m_dbOpenInte = dbOpenInte;
		m_nVolumn = vol;
		m_nSec = sec;
	}
	float m_fPrice;
	double m_dbOpenInte;
	int m_nVolumn;
	int m_nSec;
}FUPRI_VOL;

class CFixedNatWholeParam{//单合约数据参数
public:
	CFixedNatWholeParam(){ Initial(); }
	~CFixedNatWholeParam(){}
	void Initial(){
		m_dbMaxPrc = m_dbMinPrc = m_dbMinVol = m_dbMaxVol = m_dbMinOpenIn = m_dbMaxOpenIn = 0.0f;
		m_nValidCount = 0;
	}
public:
	double m_dbMaxPrc, m_dbMinPrc; //价格
	double m_dbMinVol, m_dbMaxVol; //现手
	double m_dbMinOpenIn, m_dbMaxOpenIn; //持仓量
	int m_nValidCount; //有效数据个数
};

class CFixedNatFuturePoint{
public:
	CFixedNatFuturePoint();
	~CFixedNatFuturePoint();
	CFixedNatFuturePoint& operator=(const CFixedNatFuturePoint& obj);
	void Clear();
	void Update(float fPrc, int nVol, double dbOpenInt, const char* cTime,
		int nSec, bool isFill = false, int nPosition = 0, bool bRealTime = false, const QString& strTF = "");
	bool IsFillPrice() const; //是否是填充的
protected:
	void MergePrc();
	int LoadSecTime(const char* cTime, int nSec);
public:
	float m_fPrice;      //价格
	double m_dbOpenInt;  //持仓量
	int m_nVolum;		 //现手
protected:
	std::map<int, FUPRI_VOL> m_mapPriceReal; //请求和推送的行情
};

class CFixedNatFutureTFData{
public:
	CFixedNatFutureTFData(bool needInit=true);
	CFixedNatFutureTFData(const xQBTFPriceUnit_c& unit);
	CFixedNatFutureTFData& operator = (const CFixedNatFutureTFData& obj);
	~CFixedNatFutureTFData() = default;

	void ResetDate();
	void Clear();
	void PushData(const xQBTFPriceUnit_c& unt);
	void SetTick(xQBTFTickList_c* pTick);
	void SetReal(xQBTFRealTimeList_c* pReal);
	time_t GetTickBeginTime();
	void LoadWholeParam(CFixedNatWholeParam& param,bool bYestoday);
	void TimerFillFrontLoop();//定时自动添加
	void LoadFrontValid(int & nValid, const int& nBegin, float& fPrice, bool bToday);//获取上一个有效的位置,以及价格
	bool TodayIsHoliday(void) const { return m_bIsHoliday; }
protected:
	void InitialPoint();
	void SetPrice(const xQBTFPriceUnit_c& unt);
	void FillToFront(int nBegin, float fPrc, int nVol, const char* cTime, int nSec, bool bToday);//补数据
	int GetPointFromTime(const char * cTime);
public:
	QString m_strTFId;
	QString m_strCTD;
	float m_LastPrice;		//lp		最新价
	float m_PreClosePrice;	//pcp		昨收盘
	float m_fVolumn;		//成交量
	float m_Hold;			//持仓量
	time_t m_tTickMax;
	int m_nTodayDate;
	int m_nYestodayDate;
	std::map<int, CFixedNatFuturePoint> m_mapPoint, m_mapPointYes;
	CFixedNatWholeParam m_Param;
private:
	bool m_bIsHoliday;
};

class CFixedNatFutureData{
public:
	CFixedNatFutureData() = default;
	~CFixedNatFutureData() = default;

	void Clear();
	bool SetQBPrice(xQBTFPriceList_c* pPrice);
	bool PushQBPrice(xQBTFPriceList_c* pPrice);

	void SetQBTFTick(xQBTFTickList_c* pTick);
	void SetTFRealTimeData(xQBTFRealTimeList_c* pData);
	time_t GetTickBeginTime(const QString& sTFID);

	bool GetCurrentData(CFixedNatFutureTFData& tfData);
	void SetCurrentTFID(const QString& strTF){ m_strCurrentTF = strTF; }
	const QString& GetCurrentTFID();
	bool GetData(const QString& strTF, CFixedNatFutureTFData& tfData);

	int GetSortTFSize() const { return (int)m_vctSort.size(); }
	const QString& GetSortTF(int idx) const;

	void fillRealTimePoint(const QString& TFID);

private:
	QString m_strCurrentTF;
	qb::base::CSLock m_lock;
	std::map<QString, CFixedNatFutureTFData> m_mapData;
	std::vector<QString> m_vctSort;
};

class FixedNatFutureHandler : public QObject, public MessageReceiver {
	Q_OBJECT
public:
	FixedNatFutureHandler(CFixedNatFutureData* data);
	~FixedNatFutureHandler();

	void reqTFPrice();
	void cancelReqTFPrice();
	void reqRealtimeData(const QString& TFID);
	void clearData();

protected:
	void onDataArrived(const qb::SSAckMsg& msg) override;

signals:
	void recvData(int funcId);

private:
	void fillRealTimePoint(const QString& TFID);

private:
	CFixedNatFutureData* m_data;
	QBTFPriceReq_t m_prcReq;
	std::set<QString> m_setTickReq;
	eventpp::internal_::EventDispatcherBase<std::string, void(const qb::SSAckMsg*), eventpp::DefaultPolicies, void>::Handle m_handler;
};
